package platform_api.Controller;

import org.hibernate.annotations.GeneratorType;
import platform_api.Model.JsonMessage;
import org.springframework.web.bind.annotation.*;
import platform_api.Service.TradeService;

import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/trade")
public class TradeDataRestController extends Controller {

    private final TradeService tradeService;

    public TradeDataRestController(TradeService tradeService) {
        this.tradeService = tradeService;
    }

    /**
     * 获取可选字段及其含义
     *
     * @return JsonMessage
     */
    @ResponseBody
    @GetMapping("/columns")
    public JsonMessage getColumns() {
        return tradeService.getColumns();
    }

    @ResponseBody
    @GetMapping("/apple_columns")
    public JsonMessage getAppleColumns() {
        return tradeService.getAppleColumns();
    }

    /**
     * 获取交易物品种类
     *
     * @return JsonMessage
     */
    @ResponseBody
    @GetMapping("/variety")
    public JsonMessage getVarieties() {
        return tradeService.getVarieties();
    }

    /**
     * @param sTime   开始时间
     * @param eTime   结束时间
     * @param subject 目标商品
     * @param column  字段名称
     * @return JsonMessage
     * @throws ParseException Exception report
     */
    @ResponseBody
    @GetMapping("/get")
    public JsonMessage getEchartData(String sTime, String eTime, String subject, String column, String distance) throws ParseException {

        Date startTime, endTime;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        startTime = df.parse(getRequestOrDefault(sTime, "2010-1-1"));
        endTime = df.parse(getRequestOrDefault(eTime, "2010-2-1"));
        subject = getRequestOrDefault(subject, "CF001");
        column = getRequestOrDefault(column, "highest_price");
        distance = getRequestOrDefault(distance,"day");

        return tradeService.getEchartData(subject, column, startTime, endTime, distance);
    }
}
